#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>


typedef struct node{
	int val;
	struct node * next;
}Node, *pNode;


typedef struct mystack
{
	pNode pTop;
	pNode pBottom;
}Stack, *pStack;

void initStatck(pStack stack);
bool push(pStack stack, int val);
bool pop(pStack stack, int * val);
bool show_stack(pStack stack);
bool is_empty(pStack stack);
void clear(pStack stack);


void initStatck(pStack stack) {
	pNode head = (pNode)malloc(sizeof(Node));
	head->next = NULL;
	stack->pTop = head;
	stack->pBottom = head;
}

bool push(pStack stack, int val) {
	pNode newn = (pNode)malloc(sizeof(Node));
	newn->val = val;
	newn->next = stack->pTop;
	stack->pTop = newn;
	return true;
}

bool is_empty(pStack stack) {
	return stack->pTop == stack->pBottom;
}

bool pop(pStack stack, int * val) {
	if (is_empty(stack)) return false;


	pNode temp = stack->pTop;
	*val = temp->val;
	stack->pTop = temp->next;

	free(temp);
	temp = NULL;
	return true;
}

bool show_stack(pStack stack){
	pNode p = stack->pTop;
	while (p != stack->pBottom) {
		printf("%d", p->val);
		p = p->next;
	}
}

void clear(pStack stack) {
	pNode p = stack->pTop;

	while (p != stack->pBottom) {
		pNode q = p;
		free(p);
		p = q->next;
	}
	stack->pTop = stack->pBottom;
}